package vn.info.vietnaminfo.sync;

import java.util.List;

import vn.info.vietnaminfo.R;
import vn.info.vietnaminfo.RetailerActivity;
import vn.info.vietnaminfo.data.DSDiaDiem;
import vn.info.vietnaminfo.utli.GlobalVariable;
import vn.info.vietnaminfo.utli.JSONUtil;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class SyncListRetailer extends AsyncTask<String, Long, List<DSDiaDiem>> {

	private Context mContext;
	private List<DSDiaDiem> listRetailer = null;
	private ProgressDialog mProgressDialog = null;
	
	
	public SyncListRetailer(Context context)
	{
		mContext 	=	context;
	}
	
	protected void onPreExecute()
	{
		mProgressDialog = new ProgressDialog(mContext);
		mProgressDialog.setMessage(mContext.getString(R.string.txt_loading));
		mProgressDialog.show();
	}	
	
	@Override
	protected  List<DSDiaDiem> doInBackground(String... params) {
		// TODO Auto-generated method stub
		try
		{
			String url = mContext.getString(R.string.http_get_retailer) +"?location=16.0606359,108.2169165&radius=5000&rankBy=distance&sensor=false&key=AIzaSyC8Vb-BwmPNPiJzEnERL_yNR_9RoKN2wfs";
			Log.v("SyncListRetailer", "HungCX--->url = "+url);

			listRetailer = JSONUtil.getListRetailerShop(mContext, url);
			Log.v("SyncListRetailer", "HungCX--->listRetailer = "+listRetailer);
			return listRetailer;
		}
		catch(Exception ex)
		{
		}
		return null;
	}
	
	protected void onPostExecute( List<DSDiaDiem> result)
	{
		if(mProgressDialog!=null)
		{
			mProgressDialog.dismiss();
			
		}
		try
		{
			((RetailerActivity)mContext).setAdapterRetailer(result);
		}
		catch(Exception e)
		{
			Toast.makeText(mContext, "Xảy ra lỗi trong quá trình lấy dữ liệu. Xin hãy thử lại", Toast.LENGTH_SHORT).show();
		}
	}

}
